package com.hrt.manage.comments.service.Impl;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.Comments;
import com.hrt.bean.GiftDetail;
import com.hrt.bean.HRTMember;
import com.hrt.bean.Replys;
import com.hrt.manage.comments.dao.ReplysDao;
import com.hrt.manage.comments.service.CommentsService;
import com.hrt.manage.comments.service.ReplysService;
import com.hrt.manage.member.service.HRTMemberService;
import com.hrt.manage.product.service.GiftDetailService;
import com.hrt.vo.ReplysVO;

@Transactional
@Service("ReplysService")
public class ReplysServiceImpl implements ReplysService{
	
	@Autowired
	private ReplysDao replysDao;
	
	//评论
	@Autowired
	private CommentsService commentsService;
	//礼品基本信息
	@Autowired
	private GiftDetailService giftDetailService;
	
	//会员
	@Autowired
	private HRTMemberService hRTMemberService;
	/**
	 * 保存实体
	 * */
	@Override
	public void addReplys(Replys Replys) {
		this.replysDao.saveEntity(Replys);
	}

	/**
	 * 更新实体
	 * */
	@Override
	public void updateReplys(Replys Replys) {
		this.replysDao.updateEntity(Replys);
	}

	/**
	 * 删除实体
	 * */
	@Override
	public void delReplys(Long id) {
		this.replysDao.deleteEntity(id);
	}

	/**
	 * 按id查询实体
	 * */
	@Override
	public Replys getById(Long id) {
		
		return this.replysDao.getById(id);
	}

	/**
	 * 查询所有
	 * */
	@Override
	public List<Replys> searchAll() {
		
		return this.replysDao.findAll();
	}

	/**
	 * 按是否删除查询
	 */
	@Override
	public List<Replys> searchDelAll() {
		
		return this.replysDao.findDelAll();
	}

	/**
	 * 按id列表查询实体
	 * */
	@Override
	public List<Replys> getByIds(Long[] ids) {
		
		return this.replysDao.getByIds(ids);
	}

	/**分页*/
	@Override
	public Long totalCount() {
		
		return this.replysDao.totalCount();
	}
	@Override
	public List<Replys> queryByPage(int pageIndex, int pageSize) {
		
		return this.replysDao.queryByPage(pageIndex, pageSize);
	}

	/**父级下的分页*/
	@Override
	public List<Replys> queryById(Long gdId, Integer pageIndex,
			Integer pageSize) {
		List<Map<String, Object>> conditions=new ArrayList<Map<String, Object>>();
		List<Replys> list=new ArrayList<Replys>();
		Map<String, Object> con=new HashMap<String, Object>();
		con.put("field", "commentsId");
		con.put("value", gdId);
		
		Map<String, Object> delcon=new HashMap<String, Object>();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(con);
		list=replysDao.queryPageByCondition(conditions,pageIndex, pageSize);
		
		return list;
	}
	
	/**获取数据库的当前Timestamp形式的时间*/
	@Override
	public Timestamp getDate() {
		return this.replysDao.getDate();
	}

	/**删除选中的数据*/
	@Override
	public void deleteById(Long[] ids) throws Exception {
		List<Replys> delResult = this.replysDao.getByIds(ids);
		for (Replys  comm: delResult) {
			comm.setIsdelete(new Integer(1));
			this.replysDao.updateEntity(comm);
		}
	}

	/**根据评论的ID找到回复的列表*/
	@Override
	public List<Replys> getReplysBycommId(Long commentsId) {
		return this.replysDao.getReplysBycommId(commentsId);
	}
	
	/**根据id删除回复*/
	@Override
	public boolean delItemById(Long itemId) {
		Replys item = this.replysDao.getById(itemId);
		if (item != null) {
			item.setIsdelete(new Integer(1));
			this.replysDao.updateEntity(item);
			return true;
		}
		return false;
	}
	
	/**
	 * 将CommentsVO转换为Comments
	 */
	@Override
	public void updateReplys(List<ReplysVO> replyVO) {
		try {
			for(ReplysVO rv: replyVO) {
				Replys replys = new Replys();
				if(rv.getId()==null){
					BeanUtils.copyProperties(replys, rv);
					Comments comme = this.commentsService.getById(rv.getCommentsId());
					//评论
					replys.setComments(comme);
					
					//评论id
					replys.setCommentsId(rv.getCommentsId());
					
					HRTMember hrtMe = this.hRTMemberService.getById(rv.getHrtMemberId());
					//会员
					replys.setHrtMember(hrtMe);
					
					//会员id
					replys.setHrtMemberId(rv.getHrtMemberId());
					
					//会员名称
					replys.setHrtMemberName(rv.getHrtMemberName());
					
					GiftDetail gd = this.giftDetailService.getById(rv.getGdeId());
					//礼品规格
					replys.setGiftDetail(gd);
					
					//礼品规格id
					replys.setGdeId(rv.getGdeId());
					
					//礼品规格名称
					replys.setGdeName(rv.getGdeName());
					
					/**添加*/
					this.addReplys(replys);
				}else{
					BeanUtils.copyProperties(replys, rv);
					Comments comme = this.commentsService.getById(rv.getCommentsId());
					//评论
					replys.setComments(comme);
					
					//评论id
					replys.setCommentsId(rv.getCommentsId());
					
					HRTMember hrtMe = this.hRTMemberService.getById(rv.getHrtMemberId());
					//会员
					replys.setHrtMember(hrtMe);
					
					//会员id
					replys.setHrtMemberId(rv.getHrtMemberId());
					
					//会员名称
					replys.setHrtMemberName(rv.getHrtMemberName());
					
					GiftDetail gd = this.giftDetailService.getById(rv.getGdeId());
					//礼品规格
					replys.setGiftDetail(gd);
					
					//礼品规格id
					replys.setGdeId(rv.getGdeId());
					
					//礼品规格名称
					replys.setGdeName(rv.getGdeName());
					/**修改*/
					this.updateReplys(replys);
				}
			}
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

}
